package com.auto.survey.widget.swither;


import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.LinearGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.LinearInterpolator;


public class AnimatorUtils {

//    private static final String TAG = AnimatorUtils.class.getSimpleName();

    private static int thumbTop = 0;
    private static int thumbBottom = 0;
    private static int thumbLeft = 0;
    private static int thumbRight = 0;

    /**
     * 开关打开关闭动画
     * @param offsetX 开关按钮可以偏移的量
     * @param scalY y轴方向缩小量
     * @param diameter 直径
     * @param thumbMargin 开关按钮距离边界的距离
     */
    public static AnimatorSet switcher_animator(int offsetX, int scalY, int diameter, int thumbMargin, int hotx, int hoty, ShapeDrawable thumbDrawable, View view, boolean isOpen) {
//        if (offsetX == 0) {
//            return;
//        }

        ValueAnimator valueAnimator = new ValueAnimator();
        if (isOpen) {
            valueAnimator.setIntValues(0, offsetX);
        } else {
            valueAnimator.setIntValues(offsetX, 0);
        }
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int)animation.getAnimatedValue();


                int thumbDiameter = diameter - 2*thumbMargin;

                int tempValue = 0;
                if (value <= offsetX/2) {
                    tempValue = (3*value * scalY/offsetX); // 3*value 说明：单纯的增大数据数值，没有什么逻辑
                } else {
                    tempValue = (3* (offsetX - value) * scalY/offsetX);
                }

                Log.i("UISwitch", "tempValue =" + tempValue);

                thumbTop = thumbMargin + tempValue/2 + hoty/2;
                thumbBottom = thumbTop + thumbDiameter - tempValue;

                thumbLeft = value + thumbMargin + hotx/2;
                thumbRight = diameter + value - thumbMargin + hotx/2;

                Rect bounds = new Rect(thumbLeft, thumbTop, thumbRight, thumbBottom);
                thumbDrawable.setBounds(bounds);
                view.postInvalidate();
            }
        });
        AnimatorSet set = new AnimatorSet();
        set.play(valueAnimator);
        set.setInterpolator(new LinearInterpolator());
//        set.setInterpolator(new BezierInterpolator(BezierInterpolator.BezierType.FIRST_ORDER, 230, 0.48f,0.05f,0.52f,0.95f ));
        set.setDuration(230);
        set.start();

        return set;
    }

    /**
     * 开关按钮跟手动画
     */
    public static void switcher_animator_touch_move(int value, int offsetX, int scalY, int diameter, int thumbMargin, int hotx, int hoty,  ShapeDrawable thumbDrawable, View view){
        Log.i("UISwitch", "### switcher_animator_touch_move ===>  value =" + value);
        int thumbDiameter = diameter - 2*thumbMargin;
        int tempValue = 0;
        if (value <= offsetX/2) {
            tempValue = (3 * value * scalY)/offsetX;
        } else {
            tempValue = (3*(offsetX - value) * scalY/offsetX);
        }

        Log.i("UISwitch", "### switcher_animator_touch_move ===>  tempValue =" + tempValue);

        thumbTop = thumbMargin + tempValue/2 + hoty/2;
        thumbBottom = thumbTop + thumbDiameter - tempValue;
        thumbLeft = value + thumbMargin + hotx/2;
        thumbRight = diameter + value - thumbMargin + hotx/2;

        Rect bounds = new Rect(thumbLeft, thumbTop, thumbRight, thumbBottom);
        thumbDrawable.setBounds(bounds);
        view.postInvalidate();
    }

    /**
     *  开关普通模状态下打开关闭背景色渐变动画
     */
    public static AnimatorSet switcher_animator_nomal_stauts_color(boolean hasChecked, Context context, int width, int height, ShapeDrawable uiswitcherbg, AnimationEndListener listener) {
        ValueAnimator bgAnimator = new ValueAnimator();
        uiswitcherbg.getPaint().setAlpha(255);
        ValueAnimator alphaAnimator = new ValueAnimator();

        if (hasChecked) {
            alphaAnimator.setIntValues(255, 102);
            bgAnimator.setIntValues(SelectorColor.getNomalStartColor(context), SelectorColor.getNomalEndColor(context));
        } else {
            alphaAnimator.setIntValues(102, 255);
            bgAnimator.setIntValues(SelectorColor.getNomalEndColor(context), SelectorColor.getNomalStartColor(context));
        }

        bgAnimator.setEvaluator(new ArgbEvaluator());
        bgAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int)animation.getAnimatedValue();
                LinearGradient bgShapColor = new LinearGradient(0, 0, width, height, value, value, Shader.TileMode.REPEAT);
                uiswitcherbg.getPaint().setShader(bgShapColor);

            }
        });
        alphaAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int)animation.getAnimatedValue();
                uiswitcherbg.getPaint().setAlpha(value);
            }
        });
        AnimatorSet set = new AnimatorSet();
        set.play(bgAnimator);
        set.setInterpolator(new LinearInterpolator());
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (listener != null) {
                    listener.onAnimationEnd(animation);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
//        set.setInterpolator(new BezierInterpolator(BezierInterpolator.BezierType.FIRST_ORDER, 230, 0.48f,0.05f,0.52f,0.95f ));
        set.setDuration(230);
        set.start();

        return set;
    }

    public static void switcher_animator_touchmove_color(int value, float offsetX, Context context, int width, int height, ShapeDrawable uiswitcherbg) {
        ArgbEvaluator argbEvaluator = new ArgbEvaluator();
        float fraction = (float) value/ offsetX;
        Log.i("UISwitch", "### switcher_animator_touchmove_color ===>  fraction = " + fraction);
        int color = (int) argbEvaluator.evaluate(fraction, SelectorColor.getNomalStartColor(context), SelectorColor.getNomalEndColor(context));
        LinearGradient bgShapColor = new LinearGradient(0, 0, width, height, color, color, Shader.TileMode.REPEAT);
        uiswitcherbg.getPaint().setShader(bgShapColor);
        //
        //alph 255 ~102 < ---- > 102~255

        int alphaOffset1 = 102;
        if (value <= 0.4 * offsetX) {//透明度 1 ～0.4  对应的移动的偏移 也是40%
            int alpha = 255 -(int)(alphaOffset1 * fraction * 4);
            Log.i("UISwitch", "### 111---switcher_animator_touchmove_color ===>  ALPHA = " + alpha);
            uiswitcherbg.getPaint().setAlpha(alpha);
        } else {
            int alpha = (int)(255 * fraction);
            Log.i("UISwitch", "### 222--switcher_animator_touchmove_color ===>  ALPHA = " + alpha);
            uiswitcherbg.getPaint().setAlpha(alpha);
        }

    }

    public static AnimatorSet switcher_animator_disable_stauts_color(boolean hasChecked, Context context, int width, int height, ShapeDrawable uiswitcherbg, AnimationEndListener listener){
        ValueAnimator bgAnimator = new ValueAnimator();
        uiswitcherbg.getPaint().setAlpha(255);
        ValueAnimator alphaAnimator = new ValueAnimator();
        alphaAnimator.setIntValues(255, 102, 255);
        if (hasChecked) {
            bgAnimator.setIntValues(SelectorColor.getNomalStartColor(context), SelectorColor.getDisableEndColor(context));
        } else {
            bgAnimator.setIntValues(SelectorColor.getNomalEndColor(context), SelectorColor.getDisableStartColor(context));
        }

        bgAnimator.setEvaluator(new ArgbEvaluator());
        bgAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int)animation.getAnimatedValue();
                LinearGradient bgShapColor = new LinearGradient(0, 0, width, height, value, value, Shader.TileMode.REPEAT);
                uiswitcherbg.getPaint().setShader(bgShapColor);

            }
        });
        alphaAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int)animation.getAnimatedValue();
                uiswitcherbg.getPaint().setAlpha(value);
            }
        });
        AnimatorSet set = new AnimatorSet();
        set.play(bgAnimator);
        set.setInterpolator(new LinearInterpolator());
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (listener != null) {
                    listener.onAnimationEnd(animation);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
//        set.setInterpolator(new BezierInterpolator(BezierInterpolator.BezierType.FIRST_ORDER, 230, 0.48f,0.05f,0.52f,0.95f ));
        set.setDuration(230);
        set.start();

        return set;

    }

    interface AnimationEndListener{
        void onAnimationEnd(Animator animation);
    }

}
